﻿Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Text


Public Class ShowHelper

    Private WithEvents _lbi As System.Windows.Forms.ListBox


    Public Sub SaveFileDateTimeFileName(ByVal content) 
        Dim fApplicationPath As String = AppDomain.CurrentDomain.BaseDirectory  ' 应用程序所有的路径
        Dim txtPath As String = System.IO.Path.Combine(fApplicationPath, GetFileName("txt")) '文件路径


        If File.Exists(txtPath) = False Then  ' 创建文件并写入
            Using sw As StreamWriter = File.CreateText(txtPath)

                sw.Write(content)
                sw.Flush()
                sw.Close()
            End Using
        End If

      
    End Sub



    Public Sub ListBoxShowHash(ByVal _lb As ListBox, ByVal _ht As Hashtable)
        _lbi = _lb
        _lbi.Tag = _ht
        _lbi.Items.Clear()

        If _ht Is Nothing Then
            _lbi.Items.Add("HashTable 未初始化")
        Else
            For Each _entry As DictionaryEntry In _ht

                _lbi.Items.Add(_entry.Value.ToString())
            Next
        End If

    End Sub

    Public Function ArrayList2String(ByVal prefix As String, ByVal al As ArrayList) As String
        Dim str As String = ""
        For Each reg As Object In al
            str += prefix & reg.ToString() & vbCrLf
        Next
        Return str
    End Function


    Public Sub ListBoxShowArray(ByVal _lb As ListBox, ByVal _array As ArrayList)

        _lbi = _lb
        _lbi.Tag = _array
        _lbi.Items.Clear()

        If _array Is Nothing Then
            _lbi.Items.Add("数组未初始化!")
        Else
            For Each obj As Object In _array
                _lbi.Items.Add(obj.ToString())
            Next
        End If

    End Sub


    Public Sub ListBoxShowArray(ByVal _lb As ListBox)
        _lbi = _lb

        _lbi.Items.Clear()
        Dim al As ArrayList = _lb.Tag
        For Each obj As Object In al
            _lbi.Items.Add(obj.ToString())
        Next

    End Sub




    Public Sub Test() Handles _lbi.DoubleClick
        MessageBox.Show(_lbi.SelectedItem.ToString())
    End Sub

    Public Sub KeyDowns(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles _lbi.KeyDown

        Dim tplb As ListBox = sender

        If tplb Is Nothing Then

            MessageBox.Show("事件的发出者不是listbox!")

        Else

            If tplb.SelectedItem <> "" Then
                If e.KeyCode = Keys.Delete Then

                    Dim al As ArrayList = tplb.Tag
                    al.Remove(tplb.SelectedItem.ToString)
                End If
            End If
            ListBoxShowArray(tplb)

        End If
    End Sub


    Public Sub TextBoxShowArray(ByVal _tb As TextBox, ByVal _array As ArrayList)

        _tb.Text = ""

        If _array Is Nothing Then  '判断是否为空
            _tb.Text = "数组未初始化"
        Else
            Dim tpstr As String = ""
            For Each obj As Object In _array
                tpstr += obj.ToString() + System.Environment.NewLine
            Next
            _tb.Text = tpstr
        End If

    End Sub

    '匹配字符串
    Public Function RegMatString(ByVal webString As String, ByVal _reg As String) As String

        Dim reg As New Regex(_reg)
        Dim matches As MatchCollection = reg.Matches(webString)

        Dim mat_string As String = ""
        For Each mat As Match In matches
            mat_string += mat.Value & vbCrLf
        Next

        Return mat_string

    End Function


    ''' <summary>
    ''' 取得按时间取得的文件名
    ''' </summary>
    ''' <param name="ext">扩展名</param>
    ''' <returns>以这个扩展名结尾的文件名</returns>
    ''' <remarks></remarks>
    Public Function GetFileName(ByVal ext As String) As String
        Dim filename As String = ""
        filename = Now.ToString("yyyyMMddHHmmssffff")  '精确到毫秒级别的名称
        filename = filename & "." & ext
        Return filename
    End Function




    Public Function JieXie(ByVal webString As String, ByVal _regStr As String) As String
        '遍历正则列表同时进行分析
        Dim mat_string As String = ""
        Try


            Dim reg As New Regex(_regStr)
            Dim matches As MatchCollection = reg.Matches(webString)


            For Each mat As Match In matches
                mat_string += mat.Value & vbCrLf
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        Return mat_string
    End Function


    Public Function JieXie(ByVal webString As String, ByVal _regStr As Hashtable) As String
        '遍历正则列表同时进行分析
        'Dim mid_string As String = "" '中间结果
        'mid_string = webString
        'Try
        '    For Each pagerega As WebPageRegex In _regStr
        '        mid_string = pagerega.RegexMatchString(mid_string)
        '    Next
        'Catch ex As Exception
        '    MessageBox.Show(ex.Message)
        'End Try
        'Return mid_string

    End Function






    Public Function GetUrlList(ByVal patterns As String, ByVal placeholder As String, ByVal _start As Short, ByVal _end As Short, ByRef t_urlarray As ArrayList) As Boolean


        If t_urlarray Is Nothing Then
            t_urlarray = New ArrayList()
        End If

        Dim tempurl As String = ""


        If patterns.IndexOf(placeholder) = -1 Then
            '如果未找到通配符,那么就添加单个网址
            t_urlarray.Add(patterns)
        Else
            For i = _start To _end
                tempurl = patterns.Replace(placeholder, i.ToString)

                If Not t_urlarray.Contains(tempurl) Then
                    t_urlarray.Add(tempurl)
                End If
            Next

        End If

    End Function

    ''' <summary>
    ''' 在listbox中显示文件名
    ''' </summary>
    ''' <param name="path"></param>
    ''' <param name="_listbox"></param>
    ''' <remarks></remarks>
    Public Sub ShowFolderFile(ByVal path As String, ByVal _listbox As ListBox)
        _listbox.Items.Clear()
        _listbox.SelectionMode = SelectionMode.MultiExtended
        _listbox.ScrollAlwaysVisible = True  '显示滚动框
        Dim _files As String() = Directory.GetFiles(path)

        For Each t_file As String In _files
            _listbox.Items.Add(t_file)
        Next

    End Sub



End Class